﻿using System;
using System.Data.OleDb;

namespace DAO
{
    public class DataProvider
    {
        private string connectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = NhanVien.mdb";
        private OleDbConnection connection;

        public DataProvider()
        {
            try
            {
                connection = new OleDbConnection(connectionString);
            }
            catch (OleDbException ex)
            {
                throw new Exception("Khởi tạo kết nối thất bại !", ex);
            }
        }

        public void Connect()
        {
            try
            {
                connection.Open();
            }
            catch (OleDbException ex)
            {
                throw new Exception("Kết nối thất bại, kiểm tra file dữ liệu", ex);
            }
        }

        public void Disconnect()
        {
            try
            {
                connection.Close();
            }
            catch(OleDbException ex)
            {
                connection = null;
            }
        }

        public OleDbDataReader ExeReader(string query)
        {
            try
            {
                OleDbCommand command = new OleDbCommand(query, connection);
                return command.ExecuteReader();
            }
            catch (OleDbException ex)
            {
                throw new Exception("Thực hiện truy vấn thất bại", ex); 
            }
        }

        public void ExeNonQuery(string query)
        {
            try
            {
                OleDbCommand command = new OleDbCommand(query, connection);
                command.ExecuteNonQuery();
            }
            catch (OleDbException ex)
            {
                throw new Exception("Thực hiện truy vấn thất bại", ex); 
            }
        }
    }
}
